/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/24
 * Time: 05:50
 */
$(function(){
    $.ajax({
        type: 'get',
        url: '/categories.json',
        success: function(e){
            var data = JSON.parse(e);
            if (data.status == 200) {
                var json = data.body.data;
                if (json.length > 0) {
                    $.each(json, function(n, v){
                        var html = '<li class=""><a data-toggle="tab" href="#tab-'+(n+2)+'" data-cate="'+v.id+'" aria-expanded="false">'+ v.name+'</a></li>';
                        $('.cate-list').append(html);
                    });
                }
            }
        }
    });

    var url = '/v1/teacher/work/list.json';
    var authorization = Cookies.get('_token');
    var page = 1;
    var page_size = 12;
    var data = {
        page: page,
        page_size: page_size,
        order: 'desc'
    };

    getDatas(url, data, authorization);

    $(document).on('click', '.cate-list a', function(){
        if ($(this).attr('data-cate')) {
            data.category_id = $(this).attr('data-cate');
        } else {
            data.category_id = null;
        }
        getDatas(url, data, authorization);
    });
    $(document).on('click', '.paging_simple_numbers a', function(){
        data.page = $(this).attr('data-page');
        data.category_id = $('.cate-list .active a').attr('data-cate');
        getDatas(url, data, authorization);
    });
});

function getDatas(url, data, authorization) {
    $.ajax({
        type: 'get',
        url: url,
        data: data,
        headers: {
            Authorization: authorization,
            Accept: "application/json; charset=utf-8"
        },
        success: function(e){
            var data = JSON.parse(e);
            if (data.status == 200) {
                var json = data.body.data;
                $('.work-list').html('<div class="row text-center">暂无数据</div>');
                if (json.rows.length > 0) {
                    var count = json.rows.length;
                    var rows = Math.ceil(count / 3);
                    var cols = count > 3 ? 3 : count;
                    var html = '';
                    for (var i = 0; i < rows; i ++) {
                        html += '<div class="row"><div class="col-sm-12">';
                        for (var j = 0; j < cols; j ++) {
                            if (3*i+j < count) {
                                html += '<div class="col-sm-4">';
                                html += '<div class="panel panel-default">';
                                html += '<div class="panel-heading work-name">'+json.rows[3*i+j].title+'</div>';
                                html += '<div class="panel-body">';
                                html += '<p><img src="'+json.rows[3*i+j].pics[0]+'" title="'+json.rows[3*i+j].title+'" style="width:240px;height:auto;"></p>';
                                html += '<p>作者:<span class="author">'+json.rows[3*i+j].realname+'</span></p>';
                                html += '</div><div class="panel-footer text-right"><a href="/teacher/detail/'+json.rows[3*i+j].id+'.html">评分</a></div></div></div>';
                            }
                        }
                        html += '</div></div>';
                    }
                    $('.work-list').html(html);
                    var page = '<div class="row">';
                    page += '<div class="col-sm-6">';
                    if (json.rows.length > 0) {
                        page += '<div class="dataTables_info" role="alert" aria-live="polite" aria-relevant="all">显示 <span class="data-from">'+(json.offset+1)+'</span> 到 <span class="data-to">'+((json.page_number-1)*json.page_size+count)+'</span> 项，共 <span class="data-total">'+json.total+'</span> 项</div>';
                    }
                    page += '</div>';
                    if (json.pages > 1) {
                        page += '<div class="col-sm-6">';
                        page += '<div class="dataTables_paginate paging_simple_numbers">';
                        page += '<ul class="pagination" role="navigation">';

                        if (json.page_number == 1) {
                            page += '<li class="page-item disabled" aria-disabled="true" aria-label="pagination.previous"><span class="page-link" aria-hidden="true">‹</span></li>';
                        } else {
                            page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+(json.page_number-1)+'" rel="previous" aria-label="pagination.previous">‹</a></li>';
                        }
                        for (var k = 1; k <= json.pages; k ++) {
                            if (k == json.page_number) {
                                page += '<li class="page-item active" aria-current="page"><span class="page-link">'+k+'</span></li>';
                            } else {
                                page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+k+'">'+k+'</a></li>';
                            }
                        }

                        if (json.page_number < json.pages) {
                            page += '<li class="page-item"><a class="page-link" href="javascript:;" data-page="'+(json.page_number+1)+'" rel="next" aria-label="pagination.next">›</a></li>';
                        } else {
                            page += '<li class="page-item disabled" aria-disabled="true" aria-label="pagination.next"><span class="page-link" aria-hidden="true">›</span></li>';
                        }

                        page += '</ul>';
                        page += '</div>';
                        page += '</div>';
                    }
                    page += '</div>';
                    $('.work-list').append(page);
                }
            }
        }
    });
}